package cn.lijiajia3515.cairo.system.modules.dictionary;

import cn.lijiajia3515.cairo.core.page.Page;
import cn.lijiajia3515.cairo.security.oauth2.jwt.CairoJwtPrincipal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * dictionary api
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/dictionary")
public class DictionaryApi {
	private final DictionaryService dictionaryService;

	public DictionaryApi(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	/**
	 * dictionary save
	 *
	 * @param principal principal
	 * @param param     param
	 * @return dictionary optional
	 */
	@PostMapping("/save")
	@PreAuthorize("isAuthenticated()")
	public Optional<Dictionary> save(@AuthenticationPrincipal CairoJwtPrincipal principal,
									 @RequestBody @Validated @Valid SaveDictionaryParam param) {
		String client = principal.getClient();
		return dictionaryService.save(client, param);
	}

	/**
	 * dictionary modify
	 *
	 * @param principal principal
	 * @param param     param
	 * @return dictionary optional
	 */
	@PutMapping("/modify")
	@PreAuthorize("isAuthenticated()")
	public Optional<Dictionary> modify(@AuthenticationPrincipal CairoJwtPrincipal principal,
									   @RequestBody @Valid @Validated DictionaryModifyParam param) {
		String client = principal.getClient();
		return dictionaryService.modify(client, param);
	}

	/**
	 * 删除字典
	 *
	 * @param principal 当前对象
	 * @param params    删除参数
	 * @return 字典集合
	 */
	@DeleteMapping("/delete")
	@PreAuthorize("isAuthenticated()")
	public List<Dictionary> delete(
		@AuthenticationPrincipal CairoJwtPrincipal principal,
		@RequestBody @Valid @Validated DeleteDictionaryParam params) {
		String client = principal.getClient();
		return dictionaryService.delete(client, params);
	}

	/**
	 * 插入字典项
	 *
	 * @param principal 当前账号
	 * @param param     插入字典项参数
	 * @return 当前字典
	 */
	@PutMapping("/put_item")
	@PreAuthorize("isAuthenticated()")
	public Optional<Dictionary> putItems(@AuthenticationPrincipal CairoJwtPrincipal principal,
										 @RequestBody @Valid @Validated PutDictionaryItemParam param) {
		String client = principal.getClient();
		return dictionaryService.putItems(client, param);
	}

	/**
	 * 修改字典项
	 *
	 * @param principal 当前用户
	 * @param param     参数
	 * @return 修改后的字典项
	 */
	@PatchMapping("/modify_item")
	@PreAuthorize("isAuthenticated()")
	public Optional<Dictionary> modifyItems(@AuthenticationPrincipal CairoJwtPrincipal principal,
											@RequestBody @Valid @Validated ModifyDictionaryItemParam param) {
		String client = principal.getClient();
		return dictionaryService.modifyItem(client, param);
	}

	/**
	 * 删除字典项
	 *
	 * @param principal 当前账号
	 * @param param     参数
	 * @return 删除后的字典项
	 */
	@DeleteMapping("/delete_item")
	@PreAuthorize("isAuthenticated()")
	public Optional<Dictionary> deleteItems(@AuthenticationPrincipal CairoJwtPrincipal principal,
											@RequestBody @Valid @Validated DeleteDictionaryItemParam param) {
		String client = principal.getClient();
		return dictionaryService.deleteItems(client, param);
	}

	/**
	 * 字典 列表
	 *
	 * @param principal 当前对象
	 * @param param     查询参数
	 * @return 字典集合
	 */
	@PostMapping("/find")
	@PreAuthorize("isAuthenticated()")
	public List<Dictionary> find(@AuthenticationPrincipal CairoJwtPrincipal principal,
								 @RequestBody @Valid @Validated FindDictionaryParam param) {
		String client = principal.getClient();
		return dictionaryService.find(client, param);
	}

	/**
	 * 字典 分页列表
	 *
	 * @param principal 当前账号
	 * @param param     查询参数
	 * @return 字典分页集合
	 */
	@PostMapping("/find_page")
	@PreAuthorize("isAuthenticated()")
	public Page<Dictionary> findPage(@AuthenticationPrincipal CairoJwtPrincipal principal,
									 @RequestBody @Valid @Validated FindDictionaryParam param) {
		String client = principal.getClient();
		return dictionaryService.findPage(client, param);
	}

	/**
	 * 查询单个字典
	 *
	 * @param principal 当前对象
	 * @param id        字典标识
	 * @return 字典
	 */
	@GetMapping("/find_id/{id}")
	@PreAuthorize("isAuthenticated()")
	public Optional<Dictionary> findById(@AuthenticationPrincipal CairoJwtPrincipal principal, @PathVariable String id) {
		String client = principal.getClient();
		return dictionaryService.find(client, id);
	}

}
